package com.xinqi.common.third.oss.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.xinqi.common.third.util.UuidUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class AliYunOssUtil {

    private static String screekKey = "JRytVZhuB89f3ETeH1udYXznfMQK7R";
    private static String accessKey = "LTAI5tPkfy1BZy6hD94Meo9u";
    private static String endpoint = "oss-cn-hangzhou.aliyuncs.com";
    private static String bucketName = "xinqiclass";
    private static String ossUrl = "https://xinqiclass.oss-cn-hangzhou.aliyuncs.com";

    private static String COURSE = "course";

    private static final String TEMPORARY_PATH = "temp";

    /**
     * 本地文件上传到阿里OSS
     */
    public static List<String> uploadFile(List<String> filePaths, String ossFolderPath) {
        List<String> ossUrls = new ArrayList<>();
        String uuid = UuidUtils.getUUID32().replace("-", "");
        try {
            if (filePaths != null && filePaths.size() > 0) {
                OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, screekKey);
                for (String path : filePaths) {
                    File file = new File(path);
                    //获取上传文件输入流
                    InputStream inputStream = new FileInputStream(path);
                    //获取文件名称
                    String fileName = file.getName();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    String datePath = simpleDateFormat.format(new Date());
                    //拼接
                    String targetFileName = datePath + "/" + ossFolderPath + "/" + uuid + "/" + fileName;
                    ossClient.putObject(bucketName, targetFileName, inputStream);
                    ossUrls.add(ossUrl + "/" + targetFileName);
                }
            }
        } catch (Exception e) {
            return null;
        }
        return ossUrls;
    }

    /**
     * 单个本地文件上传到阿里OSS
     */
    public static String uploadFileStream(InputStream stream, String ossFolderPath, String ext) {
        String uuid = UuidUtils.getUUID32().replace("-", "");
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, screekKey);
        //获取文件名称
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String datePath = simpleDateFormat.format(new Date());
        //拼接
        String targetFileName = datePath + "/" + ossFolderPath + "/" + uuid + "." + ext;
        ossClient.putObject(bucketName, targetFileName, stream);
        return ossUrl + "/" + targetFileName;
    }

    /**
     * 批量-本地文件上传到阿里OSS
     */
    public static List<String> uploadFileStreamList(List<InputStream> streamList, String ossFolderPath, String ext) {
        List<String> ossUrls = new ArrayList<>();
        String uuid = UuidUtils.getUUID32().replace("-", "");
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, screekKey);
        for (int i = 0; i < streamList.size(); i++) {
            //获取文件名称
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String datePath = simpleDateFormat.format(new Date());
            //拼接
            String targetFileName = datePath + "/" + ossFolderPath + "/" + uuid + "/" + i + "." + ext;
            ossClient.putObject(bucketName, targetFileName, streamList.get(i));
            ossUrls.add(ossUrl + "/" + targetFileName);
        }
        return ossUrls;
    }

}
